/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;

import javax.swing.JCheckBox;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.ServiceType;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.util.TextField;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;

public class ServiceTypeEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;

	private TextField commentField;
	private final VinnyController controller;
	private ServiceType serviceType;
	private JCheckBox serviceTypeActive;
	private TextField serviceTypeNameField;
	private final VinnyUser user;

	public ServiceTypeEditor(final VinnyController controller, final VinnyUser user) {
		this(controller, user, null);
	}

	/**
	 * @param controller
	 * @param volunteer
	 * @param serviceType
	 */
	public ServiceTypeEditor(final VinnyController controller, final VinnyUser user, final ServiceType serviceType) {
		this.controller = controller;
		this.user = user;
		this.serviceType = serviceType;
		this.initComponents();
	}

	public ServiceType getServiceType() {
		return this.serviceType;
	}

	@Override
	public String getTitle() {
		return "Service Type Editor";
	}

	@Override
	public boolean saveAction() {
		if (this.serviceType == null) {
			this.serviceType = new ServiceType();
		}

		if (!this.verify()) {
			return false;
		}

		this.serviceType.setActive(this.serviceTypeActive.isSelected());
		this.serviceType.setName(this.serviceTypeNameField.getText());
		this.serviceType.setComment(this.commentField.getText());
		this.saveServiceType();
		return true;
	}

	private void initComponents() {

		this.serviceTypeActive = new JCheckBox();
		this.serviceTypeActive.setName("Active?");
		this.serviceTypeActive.setSelected(this.serviceType != null ? this.serviceType.isActive() : true);

		this.serviceTypeNameField = new TextField(this.serviceType != null ? this.serviceType.getName() : "");
		this.serviceTypeNameField.setName("Service Type Name");

		this.commentField = new TextField(this.serviceType != null ? this.serviceType.getComment() : "");
		this.commentField.setName("Comment");

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.black, this.serviceTypeActive, this.serviceTypeNameField, this.commentField));
		this.setVisible(true);
	}

	private void saveServiceType() {
		final ApiResult<ServiceType> result = this.controller.saveServiceType(this.serviceType, this.user);
		if (!result.isError()) {
			this.serviceType = result.getResult();
		}
	}

	private boolean verify() {
		return true;

	}
}
